package com.example.demo810.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author lzh
 * @date 2020/8/12 - 9:14
 * https://juejin.im/post/6844904037133844494
 * 很多时候当我们完成某些业务后需要给用户推送相关消息提醒。
 * 对于这种非核心业务功能我们可以拿出来，创建一个事件去异步执行，从而实现核心业务和子业务的解耦
 */
@Slf4j
@Component
public class AsyncNoticeEventListener implements ApplicationListener<AsyncNoticeEvent> {

    /**
     * @Async 当前方法使用异步
     * @param asyncNoticeEvent
     */
    @Async
    @Override
    public void onApplicationEvent(AsyncNoticeEvent asyncNoticeEvent) {
        log.info("listener get event, sleep 2 second...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("event message is : {}",asyncNoticeEvent.getMessage());
    }
}
